module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('plan_permissions', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      plan_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'plans',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      permission_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'permissions',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // 添加唯一索引，确保套餐-权限组合唯一
    await queryInterface.addIndex('plan_permissions', ['plan_id', 'permission_id'], {
      unique: true,
      name: 'plan_permissions_plan_id_permission_id_unique',
    });

    // 添加索引
    await queryInterface.addIndex('plan_permissions', ['plan_id']);
    await queryInterface.addIndex('plan_permissions', ['permission_id']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('plan_permissions');
  },
};

